package com.cynen.crawler;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * 连接池的使用.
 */


public class HttpClientPoolTest {
    public static void main(String[] args) {
        // 创建连接池管理器
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        // ----配置链接池.
        //1. 设置最大连接数.
        cm.setMaxTotal(100);
        // 2.设置主机分配最大连接数
        cm.setDefaultMaxPerRoute(10); // 给指定主机最大分配10个链接,为了应对多主机爬虫.

        // 使用连接池发起请求.
        doGet(cm);
        doGet(cm);
    }

    //
    private static void doGet(PoolingHttpClientConnectionManager cm) {
        // 使用连接池,获取链接对象.而不是手工创建.
        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();

        // 发起http链接请求.
        HttpGet httpGet = new HttpGet("http://www.itcast.cn");

        CloseableHttpResponse httpResponse = null;
        try {
            httpResponse = httpClient.execute(httpGet);
            if (httpResponse.getStatusLine().getStatusCode() == 200){
                String content = EntityUtils.toString(httpResponse.getEntity(),"utf8");
                System.out.println(content.length());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            // 关闭资源.
            try {
                httpResponse.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            // 千万不能关闭httpclient.其是连接池中的对象.
            // httpClient.close();
        }

    }
}
